package com.mass.core.sys.role.services.impl;

import java.util.List;

import com.mass.core.sys.authority.dao.SysAuthorityMapper;
import com.mass.core.sys.authority.model.SysAuthority;
import com.mass.core.sys.authority.model.SysAuthorityExample;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mass.core.sys.role.dao.SysRoleMapper;
import com.mass.core.sys.role.model.SysRole;
import com.mass.core.sys.role.model.SysRoleExample;
import com.mass.core.sys.role.services.SysRoleServices;
import com.mass.core.utils.AjaxResponse;
import com.mass.core.utils.DateUtils;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

@Service
public class SysRoleServicesImpl implements SysRoleServices {

	@Autowired
	private SysRoleMapper roleMapper;      //角色dao 接口

	@Autowired
	private SysAuthorityMapper sysAuthorityMapper; //权限接口
	
	
	public List<SysRole> findRoleByUserId(Long userId) {
		return roleMapper.findRoleByUserId(userId);
	}

	
	public AjaxResponse findRoleList(SysRole sysRole, Integer pageIndex,
			Integer pageSize) {
		SysRoleExample sysRoleExample = new SysRoleExample();
		SysRoleExample.Criteria criteria = sysRoleExample.createCriteria();
		criteria.andDelFlagEqualTo(0);
		if(!ObjectUtils.isEmpty(sysRole)){
			if(!ObjectUtils.isEmpty(sysRole.getRoleCode()))
				criteria.andRoleCodeLike("%"+sysRole.getRoleCode()+"%");
			if(!ObjectUtils.isEmpty(sysRole.getRoleName()))
				criteria.andRoleNameLike("%"+sysRole.getRoleName()+"%");
		}
		if(!ObjectUtils.isEmpty(pageIndex)){
			sysRoleExample.setPageIndex((pageIndex-1)*pageSize);
			sysRoleExample.setPageSize(pageSize);
		}
		
		List<SysRole> sysRoleList = roleMapper.selectByExample(sysRoleExample);
	 	Long count = roleMapper.countByExample(sysRoleExample);
	 	
		return AjaxResponse.success("ok", sysRoleList, count, pageIndex);
	}

	//查询所有权限
	
	public AjaxResponse findAutorityAll(Long roleId) {
		return AjaxResponse.success(roleMapper.findAutorityAll(roleId,(long)0));
	}

	/**
	 * 修改角色
	 * @param sysAuthorityList  权限信息
	 * @param sysRole  角色信息
	 * @return
	 */
	
	@Transactional
	public AjaxResponse editSysAuthority(List<SysAuthority> sysAuthorityList, SysRole sysRole) {

		if(!ObjectUtils.isEmpty(sysAuthorityList)) {
			SysAuthorityExample sysAuthorityExample = new SysAuthorityExample();
			sysAuthorityExample.createCriteria().andTypesEqualTo(1).andRoleOrUserIdEqualTo(sysRole.getId());
			//删除权限
			sysAuthorityMapper.deleteByExample(sysAuthorityExample);
			//批量添加权限
			sysAuthorityMapper.insertByBatch(sysAuthorityList);
		}
		return AjaxResponse.success(roleMapper.updateByPrimaryKeySelective(sysRole));
	}

	/**
	 * 添加角色
	 * @param sysRole
	 * @return
	 */
	
	@Transactional
	public AjaxResponse addSysAuthority(SysRole sysRole) {
		sysRole.setCreateTime(DateUtils.getDate());
		int count = roleMapper.insertSelective(sysRole);
		if(count>0){
			List<SysAuthority> sysAuthorityList = sysRole.getSysAuthorityList();
			if(!ObjectUtils.isEmpty(sysAuthorityList))
			for(SysAuthority sysAuthority:sysAuthorityList){
				sysAuthority.setRoleOrUserId(sysRole.getId());
			}
			//批量添加权限
			sysAuthorityMapper.insertByBatch(sysAuthorityList);
		}
		return AjaxResponse.success(count);
	}
	
	public AjaxResponse findRoleListTheOnly(SysRole sysRole, Integer pageIndex,
			Integer pageSize) {
		SysRoleExample sysRoleExample = new SysRoleExample();
		SysRoleExample.Criteria criteria = sysRoleExample.createCriteria();
		criteria.andDelFlagEqualTo(0);
		if(!ObjectUtils.isEmpty(sysRole)){
			if(!ObjectUtils.isEmpty(sysRole.getRoleCode()))
				criteria.andRoleCodeEqualTo(sysRole.getRoleCode());
			if(!ObjectUtils.isEmpty(sysRole.getRoleName()))
				criteria.andRoleNameEqualTo(sysRole.getRoleName());
		}
		if(!ObjectUtils.isEmpty(pageIndex)){
			sysRoleExample.setPageIndex((pageIndex-1)*pageSize);
			sysRoleExample.setPageSize(pageSize);
		}
		
		List<SysRole> sysRoleList = roleMapper.selectByExample(sysRoleExample);
	 	Long count = roleMapper.countByExample(sysRoleExample);
	 	
		return AjaxResponse.success("ok", sysRoleList, count, pageIndex);
	}
	
}
